/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2022-05-13 10:03:51
 * @LastEditors: Clark
 * @LastEditTime: 2022-10-14 14:00:11
 * @Description: file content
 */

#ifndef TIMER_H
#define TIMER_H

#include <iostream>
#include <chrono>
#include "debug_log.h"

/**
 * @brief 运行时间测试类
 */
class Timer
{
public:
    Timer()
    {
        timeout = 0;
        lastTime = std::chrono::steady_clock::now();
    }

    Timer(int64_t timeout)
    {
        this->timeout = timeout;
    }

    void start()
    {
        lastTime = std::chrono::steady_clock::now();
    }

    void stop(const char *info)
    {
        log_debug("JH test performance [%s]:%ld ms",
            info,
            std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - lastTime).count()/1000);
    }

    void set_timeout(int64_t timeout)
    {
        this->timeout = timeout;
    }

    bool is_timeout()
    {
        int64_t durationTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - lastTime).count()/1000;
        if (durationTime < timeout)
        {
            // log_debug("JH --- durationTime[%lld] < timeout[%lld]", durationTime, timeout);
            return false;
        }

        return true;
    }

private:
    int64_t timeout;
    std::chrono::time_point<std::chrono::steady_clock> lastTime;
};

#endif
